<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchObj" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.hoscode" placeholder="医院编号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList()" icon="el-icon-search"
          >查询</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="deleteByIdList">批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="list"
      stripe
      style="width: 100%"
      empty-text="查无数据"
      @selection-change="handleSelectionChange"
      >>
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column prop="hosname" label="医院名称" />
      <el-table-column prop="hoscode" label="医院编号" />
      <el-table-column prop="apiUrl" label="api基础路径" width="200" />
      <el-table-column prop="contactsName" label="联系人姓名" />
      <el-table-column prop="contactsPhone" label="联系人手机" />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">可用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === 1"
            size="mini"
            type="warning"
            icon="el-icon-lock"
            @click="lockAndUnlock(scope.row.id, 0)"
            >锁定</el-button
          >
          <el-button
            v-if="scope.row.status === 0"
            size="mini"
            type="success"
            icon="el-icon-unlock"
            @click="lockAndUnlock(scope.row.id, 1)"
            >解锁</el-button
          >
          <router-link :to="'/hospSet/edit/' + scope.row.id">
            <el-button size="mini" type="primary" icon="el-icon-edit"
              >编辑</el-button
            >
          </router-link>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[1, 10, 20, 50]"
      :page-size="limit"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
  </div>
</template>

<script>
import hospset from "@/api/hospset";

export default {
  name: "HospSetList",
  data() {
    return {
      current: 1,
      limit: 10,
      total: 0,
      searchObj: {},
      list: [],
      multipleSelection: [],
    };
  },

  components: {},

  methods: {
    handleSizeChange(size) {
      this.limit = size;
      this.getList();
    },
    handleCurrentChange(page) {
      this.current = page;
      this.getList(page);
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    getList(page) {
      if (page) {
        this.current = page;
      }
      hospset
        .getHospSetList(this.current, this.limit, this.searchObj)
        .then((res) => {
          this.list = res.data.records;
          this.total = res.data.total;
          this.current = res.data.current;
          this.limit = res.data.size;
        })
        .catch((error) => {});
    },
    deleteById(id) {
      this.$confirm("此操作将永久删除该医院设置, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          hospset.deleteHospSet(id).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList(1);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteByIdList() {
      this.$confirm("此操作将永久删除选中的医院设置, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let idList = [];
          this.multipleSelection.forEach((s) => {
            idList.push(s.id);
          });
          hospset.batchDeleteHospSet(idList).then((res) => {
            this.$message({
              type: "success",
              message: "批量删除成功!",
            });
            this.getList(1);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    lockAndUnlock(id, status) {
      hospset.lockHospitalSet(id, status).then((res) => {
        this.getList();
      });
    },
  },

  created() {
    this.getList();
  },
};
</script>
<style scoped>
.pagination {
  padding: 30px 0;
  text-align: center;
}
</style>